<?php
/*排名类*/
class RangeAction extends Action {
	public function index() {
		$result = $this->getData ();
		$this->assign('range',$result['return']);
        $this->assign('average',$result['average']);
        $this->assign('score',$result['score']);
        $this->assign('count',$result['count']);
		$this->display ();
	}
	/*获取数据*/
	private function getData() {
		$sql = "select SUM(item_score) as score,SUM(item_num) as num ,vote_class.class_ID,class_name from vote_item,vote_class where vote_item.class_ID=vote_class.class_ID  GROUP BY class_ID";
		$item_group = M ()->query ( $sql ); //item按class_id分组显示
		$return = array ();
        $all_score=0;
        $all_count=0;
		foreach ( $item_group as $key => $value ) {
			if($value ['num']==0){$item_group [$key] ['average']=0;}
			else{
                $all_score+=$value ['score'];
                $all_count+=$value ['num'];
                $item_group [$key] ['average'] = number_format ( $value ['score'] / $value ['num'], 2 ) + 0; //保留两位小数
            }
		$item_group [$key] ['width'] = $item_group [$key] ['average'] - 30;
		}
        $total_average= number_format ( $all_score / $all_count, 2 ) + 0; //保留两位小数

		$count = count ( $item_group );
		$index = 0;
		while ( $count > 0 ) {
		$MaxAvg = -1;
		$location = 0;
			foreach ( $item_group as $key => $value ) {
				if ($item_group [$key] ['average'] > $MaxAvg) {
					$MaxAvg = $item_group [$key] ['average'];
						$location = $key;
				}
			
			}
			$return [$index ++] = $item_group [$location];
			unset($item_group [$location]);
			$count = count ( $item_group );
		}
		//dump ( $return );die;
		return array('return'=>$return,'average'=>$total_average,'score'=>$all_score,'count'=>$all_count);
	}
   /*点击获取详细数据*/
	public  function  ViewDetail()
	{
		$classid=I('id');
		$score=I('score');
		//还要根据classid获取服务名称
		$sql=sprintf("select class_name from vote_class where class_ID=%s",$classid);
		$results=M()->query($sql);
		$class_name=$results[0]['class_name'];
			//dump($class_name);die;
		$this->assign("classname",$class_name);

        //获取各个评价细项的人数和平均分
        $sql = sprintf ( "select score_name,item_index  from vote_score  where class_ID=%s", $classid );
        $result2 = M ()->query ( $sql );
        $this->assign("score_item",$result2);

        //获取各个食堂的人数和平均分
		$sql = sprintf ( "select item_ID,item_count, item_name,item_ID,item_score,item_num  from vote_item  where class_ID=%s order by item_order", $classid );
		$class_items = M ()->query ( $sql );



		 $remarkcount=count($result2);//所有评价细项数目

		 $this->assign('remarkcount',$remarkcount);
		 
		 $all_class_items_average=$this->get_all_item_average($classid,$result2);

		foreach ( $class_items as $key => $value ) {
            $item_id=$value['item_ID'];
			if ($value ['item_num'] == 0) {
				$class_items [$key] ['average'] = 0;
			} else {
				$class_items [$key] ['average'] = number_format ( $value ['item_score'] / $value ['item_num'], 2 );  //计算此小项总平均分
			}
              //计算每项评价的平均分

			$class_items[$key]['score_list']=$all_class_items_average[$item_id];//$this->getAverage($value['item_ID']);

		}

		//   dump($result);die;
		$this->assign("item_list",$class_items);




		$this->display();
	}
	
	/*根据每个食堂的id获取评价细节项的平均分*/
public function getAverage($item_id) {

		$sql = sprintf ( "SELECT item_count,item_score,item_num, item_score1,item_score2,item_score3,item_score4,item_score5,item_score6,item_score7,item_score8,item_score9,item_score10,item_num1,item_num2,item_num3,item_num4,item_num5,item_num6,item_num7,item_num8,item_num9,item_num10 from vote_item    where item_ID=%s   ", $item_id );
		$result = M ()->query ( $sql );
		
		$count = $result [0] ['item_count'];
		$return = array ();


		//计算1-6项平均分
		for($i = 1; $i < $count + 1; $i ++) {



			$score = 'item_score' . $i;
			$index = 'num' . $i;
			$num = 'item_num' . $i;
			$return [$i - 1] ['index'] = $index;
			if ($result [0] [$num] == 0) {
				$return [$i - 1] ['average'] = 0;
			} else {	
				$return [$i - 1] ['average'] = number_format ( $result [0] [$score] / $result [0] [$num], 2 );
			}
			$return[$i - 1]['num']=$result [0] [$num];
		}
		//计算总平均分
		/*
		if ($result [0] ['item_num'] == 0) {
			$return [0] ['total'] = 0;
		} else {
			$return [0] ['total'] = number_format ( $result [0] ['item_score'] / $result [0] ['item_num'], 2 );
		
		}*/

	return  $return;
	}
//获取指定分类下的item项平均分
public  function  get_all_item_average($class_id,$score_list){
    $sql = sprintf ( "SELECT item_ID,item_count,item_score, item_score1,item_score2,item_score3,item_score4,item_score5,item_score6,item_score7,item_score8,item_score9,item_score10,item_num1,item_num2,item_num3,item_num4,item_num5,item_num6,item_num7,item_num8,item_num9,item_num10 from vote_item    where class_ID=%s   ", $class_id );
    $item_list = M ()->query ( $sql );

    $return=array();
    foreach(  $item_list as $key=>$value){
        $item_id=$value['item_ID'];
        foreach($score_list as $key2=>$value2){
            $index=$value2['item_index']; //score表中的索引，示例为num1  num2等

            $index=substr($index,-1,1);
            $score_index='item_score'.$index;

            $num_index='item_num'.$index;

            $score=$value[$score_index];
            $num=$value[$num_index];
            $average=$num==0?0:number_format ($score/ $num, 2 ); //计算平均值
            $return[$item_id][]=array('num'=>$num,'score'=>$score,'average'=>$average); //添加到数组中
        }
    }

    return  $return;
}
	
}